<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\ApiController;
use App\Libraries\Error;
use App\Models\Post;
use App\Models\PersonnelDynamics;
use App\Models\User;
use Firebase\JWT\JWT;
use Illuminate\Http\Request;

class PostController extends ApiController
{


    public function create(Request $request) {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];


        if ( $request->post_id) {
            $post = Post::findOrFail($request->post_id);
            $post->mtime = time();
        } else {
            $post = new Post();
            $post->ctime = time();
        }
        $post->post_name = $request->post_name;
        $post->company_id = $company_id;
        $post->post_code = $request->post_code;
        $post->organization_id = $request->organization_id;
        $post->organization_name = $request->organization_name;
        $post->post_type_id = $request->post_type_id;
        $post->post_type = $request->post_type;
        $post->post_level = $request->post_level;
        $post->post_level_up = $request->post_level_up;
        $post->post_level_low = $request->post_level_low;
        $post->uid = $uid;

        $post->save();

        return showError(Error::ERR_OK,['post_id'=>$post->post_id]);

    }

    public function show(Request $request) {
        $post = Post::findOrFail($request->post_id);
        return showError(Error::ERR_OK,['post'=>$post]);
    }

    public function index(Request $request)
    {
        if (!$request->token) {
            return showError(Error::ERR_ERROR, null, "无效的token!");
        }
        $userInfo = JWT::decode($request->token,env('JWT_KEY'), array('HS256'));
        $exp = $userInfo->exp;
        if (time() > $exp) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
        $userInfo = (array)$userInfo->data;
        $uid = $userInfo['uid'];
        $user = User::where('uid',$uid)->where('status',1)->first();
        if (empty($user)) {
            return showError(Error::ERR_TOKEN_ERROR);
        }
//        $uid = '29221';
        $company_id = $userInfo['company_id'];
        // 搜索内容
        $where = [];
        $name = $request->input('name');
        if (!empty($name))
        {
            $where['name'] = $name;
        }
        $code = $request->input('code');
        if (!empty($code))
        {
            $where['code'] = $code;
        }
//        $status = $request->input('status');
//        if ($status > 0)
//        {
        $where['company_id'] = $company_id;
//            $where['status'] = 1;
//        }

        $list = Post::getList($where);

        return showError(Error::ERR_OK,['list'=>$list]);
    }



}
